package com.zuipin.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.SysUserUnit;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.ISysUserUnitService;
import com.zuipin.util.Pagination;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户-组织 关系Controller
 * 
 * @ClassName: SysUserUnitController
 * @Description: TODO
 * @author robot
 * @date 2017年09月147日 09:21:09
 */
@Api(value = "用户-组织 关系", description = "用户-组织 关系")
@RestController
@RequestMapping(value = "/sysUserUnit")
public class  SysUserUnitController {
	@Resource
	public ISysUserUnitService sysUserUnitService;

	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ApiOperation(value = "根据主键查询用户-组织 关系", notes = "查询用户-组织 关系")
	public Result<SysUserUnit> getSysUserUnitById(@ApiParam(required = true, name = "id", value = "主键 自增长") @RequestParam Integer id){
		SysUserUnit sysUserUnit = sysUserUnitService.findById(id);
		Result<SysUserUnit> result = new Result<SysUserUnit>();
		if (sysUserUnit != null) {
			result.setData(sysUserUnit);
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setErrorCode(ErrorCode.ELSE);
		}
		return result;
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ApiOperation(value = "新增用户-组织 关系", notes = "新增用户-组织 关系")
	@Valids({
		
		@Valid(name = "sysUserUnit.userId",  maxLength = 19),
		@Valid(name = "sysUserUnit.unitId",  maxLength = 10),
	})
	@ApiImplicitParams({
		
		@ApiImplicitParam(name = "sysUserUnit.userId", value = "后台用户 sys_user表的主键", required = true, dataType = "userId", paramType = "query"),
		@ApiImplicitParam(name = "sysUserUnit.unitId", value = "组织部门 sys_unit表的主键", required = true, dataType = "unitId", paramType = "query"),
	})
	public Result<SysUserUnit> insertSysUserUnit(@Inject @ApiIgnore SysUserUnit sysUserUnit) {
		Result<SysUserUnit> result = new Result<SysUserUnit>();
		sysUserUnitService.insert(sysUserUnit);
		result.setData(sysUserUnit);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ApiOperation(value = "根据主键修改用户-组织 关系", notes = "修改用户-组织 关系")
	@Valids({ 
		@Valid(name = "sysUserUnit.id",  maxLength = 10),
		@Valid(name = "sysUserUnit.userId",  maxLength = 19),
		@Valid(name = "sysUserUnit.unitId",  maxLength = 10),
	})
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sysUserUnit.id", value = "主键 自增长", required = true, dataType = "id", paramType = "query"),
		@ApiImplicitParam(name = "sysUserUnit.userId", value = "后台用户 sys_user表的主键", required = true, dataType = "userId", paramType = "query"),
		@ApiImplicitParam(name = "sysUserUnit.unitId", value = "组织部门 sys_unit表的主键", required = true, dataType = "unitId", paramType = "query"),
	})
	public Result<SysUserUnit> updateSysUserUnit(@Inject @ApiIgnore SysUserUnit sysUserUnit) {
		Result<SysUserUnit> result = new Result<SysUserUnit>();
		sysUserUnitService.update(sysUserUnit);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/del", method = RequestMethod.DELETE)
	@ApiOperation(value = "根据主键删除用户-组织 关系", notes = "删除用户-组织 关系")
	public Result<SysUserUnit> delSysUserUnit( Integer id) {
		Result<SysUserUnit> result = new Result<SysUserUnit>();
		sysUserUnitService.delete(id);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/seach", method = RequestMethod.POST)
	@ApiOperation(value = "搜索用户-组织 关系SKU用户-组织 关系并分页", notes = "搜索用户-组织 关系并分页")
	@Valids({
		
		@Valid(name = "sysUserUnit.userId",  maxLength = 19),
		@Valid(name = "sysUserUnit.unitId",  maxLength = 10),
		@Valid(name = "page.pageNumber",  maxValue = 999),
		@Valid(name = "page.pageSize",  maxValue = 999),
	})
	@ApiImplicitParams({
		
		@ApiImplicitParam(name = "sysUserUnit.userId", value = "后台用户 sys_user表的主键", required = true, dataType = "userId", paramType = "query"),
		@ApiImplicitParam(name = "sysUserUnit.unitId", value = "组织部门 sys_unit表的主键", required = true, dataType = "unitId", paramType = "query"),
		@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
		@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"),
	})
	public Result<List<SysUserUnit>> seachSysUserUnit(@Inject @ApiIgnore SysUserUnit sysUserUnit,
			@Inject @ApiIgnore Pagination page) {
		PageResult<List<SysUserUnit>> result = new PageResult<List<SysUserUnit>>();
		result.setData(sysUserUnitService.seachSysUserUnit(sysUserUnit, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
